package com.hmall.item.job;

import cn.hutool.json.JSONUtil;
import com.hmall.common.pojo.Item;
import com.hmall.item.service.impl.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MyTask {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ItemService itemService;

    /**
     * 定时任务 每隔触发一次
     * 0 0 3 * * ?
     */
    @Scheduled(cron = "0 0 3 * * ?  ")
    public void executeTask(){
        List<Item> top20 = itemService.getTop20();
        String jsonStr = JSONUtil.toJsonStr(top20);
        redisTemplate.opsForValue().set("top20", jsonStr);
    }
}